package com.sunhao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.sunhao.myapplication.database.UserDBHelper;
import com.sunhao.myapplication.entity.User;
import com.sunhao.myapplication.utils.ToastUtil;

import java.util.List;

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText age;
    private EditText height;
    private EditText weight;
    private CheckBox married;
    private Button insert;
    private Button update;
    private Button delete;
    private Button query;
    private UserDBHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        name = findViewById(R.id.name);
        age = findViewById(R.id.age);
        height = findViewById(R.id.height);
        weight = findViewById(R.id.weight);
        married = findViewById(R.id.married);

        insert = findViewById(R.id.insert);
        update = findViewById(R.id.update);
        delete = findViewById(R.id.delete);
        query = findViewById(R.id.query);

        update.setOnClickListener(this);
        insert.setOnClickListener(this);
        delete.setOnClickListener(this);
        query.setOnClickListener(this);


    }


    @Override
    protected void onStart() {
        //初始化连接
        super.onStart();
        helper = UserDBHelper.getInstance(this);
        helper.openWriteLink();
        helper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();

        //关闭连接
        helper.closeLink();
    }


    @Override
    public void onClick(View view) {
        String et_name = name.getText().toString();
        String et_age = age.getText().toString();
        String et_height = height.getText().toString();
        String et_weight = weight.getText().toString();
        boolean checked = married.isChecked();
        //通过构造方法传入值
        User user = new User
                (et_name,
                        Integer.parseInt(et_age),
                        Long.parseLong(et_height),
                        Float.parseFloat(et_weight),
                        checked);
        switch (view.getId()) {
            case R.id.insert:

                long insert = helper.insert(user);
                System.out.println(insert);
                if (insert > 0) {
                    //如果操作成功,就给出提示
                    ToastUtil.show(this, "添加成功");
                }

                break;
            case R.id.update:
                long update = helper.update(user);
                if(update>0){
                    //如果操作成功,就给出提示
                    ToastUtil.show(this, "修改成功");
                }
                break;
            case R.id.delete:
                long delete = helper.deleteByName(et_name);
                if(delete>0){
                    //如果操作成功,就给出提示
                    ToastUtil.show(this, "删除成功");
                }
                break;
            case R.id.query:
                List<User> query = helper.query();
              for (User u:query){
                  System.out.println(u.toString());
              }
                break;
        }
    }


}